﻿// 13. Write a program that can solve these tasks:
// Reverses the digits of a number (1)
// Calculates the average of a sequence of integers (2)
// Solves a linear equation a * x + b = 0 (3)
//		Create appropriate methods.
//		Provide a simple text-based menu for the user to choose which task to solve.
//		Validate the input data:
// The decimal number should be non-negative
// The sequence should not be empty
// a should not be equal to 0

using System;

class MenuProgram
{
    static void Main()
    {
        Console.WriteLine("1 is for reversing a number");
        Console.WriteLine("2 is for calculating the average sum of integers");
        Console.WriteLine("3 is for solving the linear equation a * x + b = 0");
        Console.Write("Enter your choice : ");
        int choice = int.Parse(Console.ReadLine());

        if (choice == 1)
        {
            Console.Write("Enter your number for reversing : ");
            int number = int.Parse(Console.ReadLine());
            if (number < 0)
            {
                Console.WriteLine("The number had to be non-negative.");
                return;
            }
            else
            {
                int reversedNumber = ReverseDecimalNumber(number);
                Console.WriteLine("And the reversed number is {0}.", reversedNumber);
            }
        }
        else if (choice == 2)
        {
            Console.Write("How many numbers will you enter : ");
            int length = int.Parse(Console.ReadLine());

            if (length <= 0)
            {
                Console.WriteLine("The list should not be empty.");
                return;
            }
            else
            {
                Console.WriteLine("Enter values of the sequence : ");
                int[] array = new int[length];
                for (int i = 0; i < length; i++)
                {
                    Console.Write("Number {0} is ", i + 1);
                    array[i] = int.Parse(Console.ReadLine());
                }
                double average = CalculateAverageOfSequence(array);
                Console.WriteLine("And the average of the sequence is : " + average);
            }
        }
        else if (choice == 3)
        {
            Console.Write("Enter coefficient a = ");
            double coefficientA = double.Parse(Console.ReadLine());
            if (coefficientA == 0)
            {
                Console.WriteLine("Coefficient A had to be different form 0!");
                return;
            }
            Console.Write("Enter coefficient b = ");
            double coefficientB = double.Parse(Console.ReadLine());
            double result = SolveLinearEquation(coefficientA, coefficientB);
            Console.WriteLine("The result of the linear equation {0} * x + {1} = 0 is {2}", coefficientA, coefficientB, result);
        }
        else
        {
            Console.WriteLine("Your choice is wrong. Start the program again, please.");
        }

    }

    static int ReverseDecimalNumber(int number)
    {
        string stringResult = "";

        while (number > 0)
        {
            int remainder = number % 10;
            number /= 10;
            stringResult += remainder;
        }

        int reversedNumber = Convert.ToInt32(stringResult);
        return reversedNumber;
    }

    static double CalculateAverageOfSequence(int[] array)
    {
        int sum = 0;
        for (int i = 0; i < array.Length; i++)
        {
            sum += array[i];
        }

        double averageOfSequence = (double)sum / array.Length;
        return averageOfSequence;
    }

    static double SolveLinearEquation(double coefficientA, double coefficientB)
    {
        double result = -coefficientB / coefficientA;
        return result;
    }
}
